package com.analyse.stock.strategy.indicator;

import org.ta4j.core.BarSeries;
import org.ta4j.core.Indicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.helpers.HighestValueIndicator;
import org.ta4j.core.indicators.helpers.LowestValueIndicator;
import org.ta4j.core.num.Num;

/**
 * Helper class: RSV Indicator
 */

public class RSVIndicator implements Indicator<Num> {
    private final ClosePriceIndicator closePrice;
    private final HighestValueIndicator highestHigh;
    private final LowestValueIndicator lowestLow;

    public RSVIndicator(Indicator<Num> closePrice, Indicator<Num> highPrice, Indicator<Num> lowPrice, int barCount) {
        this.closePrice = new ClosePriceIndicator(closePrice.getBarSeries());
        this.highestHigh = new HighestValueIndicator(highPrice, barCount);
        this.lowestLow = new LowestValueIndicator(lowPrice, barCount);
    }

    @Override
    public Num getValue(int index) {
        Num highestHighValue = highestHigh.getValue(index);
        Num lowestLowValue = lowestLow.getValue(index);
        Num closeValue = closePrice.getValue(index);

        Num denominator = highestHighValue.minus(lowestLowValue);
        if (denominator.isZero()) {
            return closePrice.numOf(0); // 避免分母为 0
        }
        return closeValue.minus(lowestLowValue).dividedBy(denominator).multipliedBy(closePrice.numOf(100));
    }

    @Override
    public BarSeries getBarSeries() {
        return closePrice.getBarSeries();
    }

    @Override
    public Num numOf(Number number) {
        return null;
    }
}
